package org.bird.jvm;

import java.lang.reflect.Field;

import sun.misc.Unsafe;  
/**
 * @Described：直接内存溢出探究 
 * @VM Args：-Xmx20M -XX:MaxDirectMemorySize=10M -XX:+PrintGCDetails
 * @author liangjf
 *
 */
@SuppressWarnings("restriction")
public class DirectoryMemoryOutOfmemory {

	private static final int _1MB = 1024 * 1024;
	private static int count = 1;

	public static void main(String[] args) {
		try {
			Field field = Unsafe.class.getDeclaredField("theUnsafe");
			field.setAccessible(true);
			Unsafe unsafe = (Unsafe) field.get(null);
			while (true) {
				unsafe.allocateMemory(_1MB);
				count++;
			}
		} catch (Exception e) {
			System.out.println("Exception:instance created " + count);
			e.printStackTrace();
		} catch (Error e) {
			System.out.println("Error:instance created " + count);
			e.printStackTrace();
		}

	}
}
